package controllers

import (
	"fis-revel-pkg/revel"
)

type base struct {
	*revel.Controller
}

// 检测用户是否登录
/*func checkLogin(c *revel.Controller) revel.Result {
	res, err := c.Session.Get("user_info")
	if err == nil {
		if userInfo, ok := res.(map[string]interface{}); ok {
			if passid, ok := userInfo["passport_id"].(float64); ok {
				uid := int(passid)
				if uid > 0 {
					return nil
				}
			}
		}
		return c.ReturnJSON(9000001, "用户不存在", nil)
	}

	accessToken := c.Request.GetHttpHeader("Dahai-Access-Token")
	if accessToken == "" {
		return c.ReturnJSON(9000001, "token lost ", nil)
	}

	client, err := passport.NewClient()
	if err != nil {
		return c.ReturnJSON(9000001, err.Error(), nil)
	}
	uid, err := client.ValidateToken(accessToken)

	if err != nil {
		c.Log.Warn("passprot err:" + err.Error())
		return c.ReturnJSON(9000001, err.Error(), nil)
	}
	userInfo := make(map[string]interface{}, 1)
	userInfo["passport_id"] = uid
	c.Session.Set("user_info", userInfo)
	return nil
}

// 获取用户ID
func (b base) getUid() (int, error) {
	result, err := b.Session.Get("user_info")
	if err != nil {
		//log
		return 0, err
	}
	if userInfo, ok := result.(map[string]interface{}); ok {
		if passid, ok := userInfo["passport_id"].(float64); ok {
			uid := int(passid)
			if uid > 0 {
				return uid, nil
			}
		}
	}
	return 0, err
}*/

func init() {
	// 登陆拦截器
	//revel.InterceptFunc(checkLogin, revel.BEFORE, &base{})
	//验签拦截器
	revel.InterceptFunc(revel.CheckApp, revel.AFTER, &base{})
}
